package com.cospandesign.gui.pubsub.widgets;

import dlcm.Structure;
import dlcm.Member;
import dlcm.DLCMListener;
import pubsub.PubSubManager;

import javax.swing.*;
import javax.swing.GroupLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.TimerTask;
import java.util.Timer;

import org.jdesktop.layout.*;

/**
 * TODO: Convert to generic function widget (maybe use JEP or an open graph library)
 * TODO: Maybe change asynchronous handling so that a clock can be connected to the widget that controls the thread event firing
 *
 * @author Michael Shareghi
 * @version Apr 25, 2010
 */
public class SinewaveWidget extends javax.swing.JFrame implements Widget {

    Class DataClass;
    dlcm.Member mMember;
    private String expression = "sin(x)";
    private String ChannelName;
    private String MemberName;
    private Structure mStructure;
    private PubSubManager Parent;

    public SinewaveWidget()
    {
        super();
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Static Functions">
    public static String GetWidgetName() {
        return "Sinewave Widget";
    }

    private void ButtonActionPerformed(ActionEvent e) {
        TimerTask generateSinewave = new GenerateSinewaveTask();
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(generateSinewave, 100, 100);
    }

    public void ReceiveMessage(Structure structure) {
        // do nothing
    }

    public boolean CanUseMember(Member member) {
            return member != null && member.Data instanceof Number;
        }

    public void SetupWidget(String channelName, String memberName, Structure structure, PubSubManager parent) {
        ChannelName = channelName;
        MemberName = memberName;
        mStructure = structure;
        mMember = mStructure.getMember(memberName);
        Parent = parent;
        setTitle(memberName + " Text Box");
        setVisible(true);
    }

    public void setWidgetVisibility(boolean visible) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    final class GenerateSinewaveTask extends TimerTask {

      Number i = 0;
      /**
      * Implements TimerTask's abstract run method.
      */
      public void run()
      {
          int val = (int)(Math.sin(i.intValue()) * Integer.MAX_VALUE);
          mStructure.getMember(MemberName).Data = val;
          System.out.println(val);
          i = i.intValue() + 1;
          Parent.PublishStructure(ChannelName, mStructure);
      }
        
    }

    private void initComponents() {

        //======== this ========
        setName("this");
        Container contentPane = getContentPane();


        JLabel lblSampleRate = new JLabel("Sample rate:");
        contentPane.add(lblSampleRate);
        
        //---- Text Box ----
        
        JTextArea txtDuration = new JTextArea();
        txtDuration.setText("100");
        contentPane.add(txtDuration);

        //---- Button ----

        JButton button = new JButton("Execute");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ButtonActionPerformed(e);
            }
        });


           javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
           
        getContentPane().setLayout(layout);

            layout.setHorizontalGroup(layout.createSequentialGroup()
                .addComponent(lblSampleRate)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(txtDuration)
                    )
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(button))
            );

        layout.setVerticalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(lblSampleRate)
                .addComponent(txtDuration)
                .addComponent(button))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
        );

        layout.linkSize(txtDuration, button);

        this.pack();
        
    }

    /**
     * @param args the command line arguments
     */
     public static void main(String args[]) {
         java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                 new SinewaveWidget().setVisible(true);
             }
         });
     }
    

}
